package fhq.hcmute.edu.vn.activities;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import fhq.hcmute.edu.vn.languages.Language;
import fhq.hcmute.edu.vn.util.SharePreferencesUtil.TouchPadSetting;

public class MouseScrollSensitivitySettingActivity extends Activity {

	private static final int MAX_SENSITIVITY = 3;
	private static final int DEFAULT_SENSITIVITY = 3;
	private SeekBar seekBar;
	private TextView txtPercent;
	private Button btnOK;
	private Button btnCancel;
	private int progress;

	@Override
	public final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Language.updateLanguageSystem(getBaseContext());
		setContentView(R.layout.activity_touchpad_sensitivity);
		seekBar = (SeekBar) findViewById(R.id.seekBar);
		seekBar.setMax(MAX_SENSITIVITY);
		txtPercent = (TextView) findViewById(R.id.lblPercent);
		btnOK = (Button) findViewById(R.id.btnOK);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		SharedPreferences preferences =
				getSharedPreferences(TouchPadSetting.TOUCHPAD_SETTING, 1);
		progress =
				preferences
						.getInt(TouchPadSetting.MOUSE_SCROLL_SENSITIVITY,
								DEFAULT_SENSITIVITY);
		seekBar.setProgress(progress);
		txtPercent.setText(progress + " ");
		btnOK.setOnClickListener(onOK);
		btnCancel.setOnClickListener(onCancel);
		seekBar.setOnSeekBarChangeListener(changeListener);
	}

	private OnSeekBarChangeListener changeListener =
			new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					if (progress != MAX_SENSITIVITY) {
						txtPercent.setText((progress + 1) + "");
					} else {
						txtPercent.setText(progress + "");
					}
				}
			};
	private OnClickListener onOK = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			progress = seekBar.getProgress();
			saveSeekBarState(progress);
			finish();
		}
	};
	private OnClickListener onCancel = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

	private void saveSeekBarState(int percent) {
		SharedPreferences preferences =
				getSharedPreferences(TouchPadSetting.TOUCHPAD_SETTING, 0);
		SharedPreferences.Editor editor = preferences.edit();
		editor.clear();
		editor.putInt(TouchPadSetting.MOUSE_SCROLL_SENSITIVITY, percent);
		editor.commit();
	}
}
